#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#include<easyx.h>
#include<stdio.h>
#include<math.h>
#define NUM_OF_BALL 100
#define PI 3.14
typedef struct {
	int x;
	int y;
	int vx;
	int vy;
	COLORREF color;
}ball;
int main()
{
	initgraph(800, 600);
	setorigin(400, 300);
	setaspectratio(1, -1);
	setbkcolor(WHITE);
	setlinecolor(BLACK);
	cleardevice();

	ball* balls = (ball*)malloc(sizeof(ball) * NUM_OF_BALL);
	if
		(balls == NULL)
	{
		return -1;
	}
	int r = 10;

	for (int i = 0; i < NUM_OF_BALL; i++)
	{
		int m, n;

		m = -400 + r;
		n = 400 - r;
		balls[i].x = rand() % (n - m + 1) + m;

		m = -300 + r;
		n = 300 - r;
		balls[i].y = rand() % (n - m + 1) + m;

		balls[i].color = HSVtoRGB((float)(rand() % 360), 0.8f, 0.9f);

		m = 3; n = 8;
		int v = rand() % (n - m + 1) + m;
		double theta;
		theta = rand() % 360;
		balls[i].vx = v * cos(theta * PI / 180);
		balls[i].vy = v * sin(theta * PI / 180);

	}
	BeginBatchDraw();
	while (1)
	{
		cleardevice();

		for (int i = 0; i < NUM_OF_BALL; i++)
		{
			setfillcolor(balls[i].color);
			fillcircle(balls[i].x, balls[i].y, r);
		}
		FlushBatchDraw();
		Sleep(40);
		for (int i = 0; i < NUM_OF_BALL; i++) {
			if (balls[i].y >= 300 - r || balls[i].y <= -300 + r)
			{
				balls[i].vy = -balls[i].vy;
			}
			if (balls[i].x >= 400 - r || balls[i].x <= -400 + r)
			{
				balls[i].vx = -balls[i].vx;
			}
			balls[i].x += balls[i].vx;
			balls[i].y += balls[i].vy;
		}

	}
	EndBatchDraw();
	closegraph();
	return 0;
}